package com.ssn.controller;

import com.ssn.po.Type;
import com.ssn.service.impl.BlogServiceImpl;
import com.ssn.service.impl.TypeServiceImpl;
import com.ssn.vo.BlogQuery;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class TypeBlogController {

    @Resource
    private TypeServiceImpl typeService;
    @Resource
    private BlogServiceImpl blogService;
    @GetMapping("/type/{id}")
    public String type(@PageableDefault(size = 10,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                       @PathVariable Long id, Model model){
        List<Type> types = typeService.listTypeTop(100);
        BlogQuery blogQuery = new BlogQuery();
        Long activeTypeId;
        if (id == -1){
            activeTypeId = types.get(0).getId();
            blogQuery.setTypeId(activeTypeId);
        }else{
            activeTypeId = id;
            blogQuery.setTypeId(activeTypeId);
        }
        model.addAttribute("page", blogService.listBlog(pageable,blogQuery));
        model.addAttribute("types", types);
        model.addAttribute("activeTypeId", activeTypeId);
        return "type";
    }
}
